home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / hydra / fmisc.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  7KB  |  222 lines

  1. /*=============================================================================
  2.                               HydraCom Version 1.00
  3. =============================================================================*/
  4.  
  5. #include "hydracom.h"
  6.  
  7. #ifdef AMIGA
  8. #define NAMELEN  32    /* OLSEN */
  9. #else
  10. #define NAMELEN  13
  11. #endif
  12.  
  13. #define LINELEN  64
  14.  
  15.  
  16. static char xfer_log[PATHLEN];
  17. static boolean xfer_logged;
  18. static char xfer_pathname[PATHLEN];
  19. static char xfer_real[NAMELEN],
  20.             xfer_temp[NAMELEN];
  21. static long xfer_fsize,
  22.             xfer_ftime;
  23.  
  24.  
  25. void unique_name (char *pathname)
  26. {
  27.         static char *suffix = ".000";
  28.         register char *p;
  29.         register int   n;
  30.  
  31.         if (fexist(pathname)) {
  32.            p = pathname;
  33.            while (*p && *p!='.') p++;
  34.            for (n=0; n<4; n++) {
  35.                if (!*p) {
  36.                   *p     = suffix[n];
  37.                   *(++p) = '\0';
  38.                }
  39.                else
  40.                   p++;
  41.            }
  42.  
  43.            while (fexist(pathname)) {
  44.                  p = pathname + ((int) strlen(pathname)) - 1;
  45.                  if (!isdigit(*p))
  46.                     *p = '0';
  47.                  else {
  48.                     for (n=3; n--;) {
  49.                         if (!isdigit(*p)) *p = '0';
  50.                         if (++(*p) <= '9') break;
  51.                         else               *p-- = '0';
  52.                     }/*for*/
  53.                  }
  54.            }/*while(exist)*/
  55.         }/*if(exist)*/
  56. }/*unique_name()*/
  57.  
  58.  
  59. char *xfer_init (char *fname, long fsize, long ftime)
  60. {
  61.         char  linebuf[LINELEN + 1];
  62.         char  bad_real[NAMELEN],
  63.               bad_temp[NAMELEN];
  64.         long  bad_fsize,
  65.               bad_ftime;
  66.         char *p;
  67.         FILE *fp;
  68.  
  69.         if (single_done)
  70.            return (NULL);
  71.  
  72.         strcpy(xfer_real,fname);
  73.         xfer_fsize = fsize;
  74.         xfer_ftime = ftime;
  75.  
  76.         mergepath(xfer_pathname,download,xfer_real);
  77.         if (fexist(xfer_pathname)) {
  78.            struct stat f;
  79.  
  80.            stat(xfer_pathname,&f);
  81.            if (xfer_fsize == f.st_size && xfer_ftime == f.st_mtime)
  82.               return (NULL);                            /* already have file */
  83.         }
  84.  
  85.         mergepath(xfer_log,download,"BAD-XFER.LOG");
  86.  
  87.         if ((fp = sfopen(xfer_log,"rt",DENY_WRITE)) != NULL) {
  88.            while (fgets(linebuf,LINELEN,fp)) {
  89.                  sscanf(linebuf,"%s %s %ld %lo",
  90.                                 bad_real, bad_temp, &bad_fsize, &bad_ftime);
  91.                  if (!strcmp(xfer_real,bad_real) &&
  92.                      xfer_fsize == bad_fsize && xfer_ftime == bad_ftime) {
  93.                     mergepath(xfer_pathname,download,bad_temp);
  94.                     if (fexist(xfer_pathname)) {
  95.                        fclose(fp);
  96.                        strcpy(xfer_temp,bad_temp);
  97.  
  98.                        xfer_logged = true;
  99.                        return (xfer_pathname);
  100.                     }
  101.                  }
  102.            }
  103.  
  104.            fclose(fp);
  105.         }
  106.  
  107.         strcpy(xfer_pathname,download);
  108.         p = xfer_pathname + ((int) strlen(xfer_pathname));
  109.         strcat(xfer_pathname,"BAD-XFER.000");
  110.         unique_name(xfer_pathname);
  111.         strcpy(xfer_temp,p);
  112.  
  113.         xfer_logged = false;
  114.         return (xfer_pathname);
  115. }/*xfer_init()*/
  116.  
  117.  
  118. boolean xfer_bad (void)
  119. {
  120.         struct stat f;
  121.         FILE *fp;
  122.         int n;
  123.  
  124.         if (single_file[0])
  125.            single_done = true;
  126.  
  127.         if (xfer_logged)                        /* Already a logged bad-xfer */
  128.            return (true);
  129.  
  130.         n = ((int) strlen(xfer_real)) - 1;
  131.         if (n > 3 &&
  132.             (!strcmp(&xfer_real[n-3],".PKT") || !strcmp(&xfer_real[n-3],".REQ"))) {
  133.            xfer_del();
  134.            return (false);                      /* don't recover .PKT / .REQ */
  135.         }
  136.  
  137.         stat(xfer_pathname,&f);
  138.         if (noresume || f.st_size < 1024L) {     /* not allowed/worth saving */
  139.            xfer_del();
  140.            return (false);
  141.         }
  142.  
  143.         if ((fp = sfopen(xfer_log,"at",DENY_WRITE)) != NULL) {
  144.            fprintf(fp,"%s %s %ld %lo\n",
  145.                      xfer_real, xfer_temp, xfer_fsize, xfer_ftime);
  146.            fclose(fp);
  147.  
  148.            return (true);                             /* bad-xfer logged now */
  149.         }
  150.  
  151.         xfer_del();
  152.         return (false);                             /* Couldn't log bad-xfer */
  153. }/*xfer_bad()*/
  154.  
  155.  
  156. char *xfer_okay (void)
  157. {
  158.         static char new_pathname[PATHLEN];
  159.         char *p;
  160.  
  161.         strcpy(new_pathname,download);
  162.         p = new_pathname + ((int) strlen(new_pathname));   /* start of fname */
  163.         if (single_file[0]) {
  164.            strcat(new_pathname,single_file);           /* add override fname */
  165.            single_done = true;
  166.         }
  167.         else {
  168.            strcat(new_pathname,xfer_real);                 /* add real fname */
  169.            unique_name(new_pathname);                      /* make it unique */
  170.         }
  171.         rename(xfer_pathname,new_pathname);           /* rename temp to real */
  172.         if (!nostamp && xfer_ftime)
  173.            setstamp(new_pathname,xfer_ftime);               /* set timestamp */
  174.  
  175.         if (xfer_logged)                         /* delete from bad-xfer log */
  176.            xfer_del();
  177.  
  178.         return (strcmp(p,xfer_real) ? p : NULL);              /* dup rename? */
  179. }
  180.  
  181.  
  182. void xfer_del (void)
  183. {
  184.         char  new_log[PATHLEN];
  185.         char  linebuf[LINELEN + 1];
  186.         char  bad_real[NAMELEN],
  187.               bad_temp[NAMELEN];
  188.         long  bad_fsize,
  189.               bad_ftime;
  190.         FILE *fp, *new_fp;
  191.         boolean left;
  192.  
  193.         if (fexist(xfer_pathname))
  194.            unlink(xfer_pathname);
  195.  
  196.         if ((fp = sfopen(xfer_log, "rt", DENY_WRITE)) != NULL) {
  197.            mergepath(new_log,download,"BAD-XFER.$$$");
  198.            if ((new_fp = sfopen(new_log, "wt", DENY_ALL)) != NULL) {
  199.               left = false;
  200.               while (fgets(linebuf,LINELEN,fp)) {
  201.                     sscanf(linebuf,"%s %s %ld %lo",
  202.                                    bad_real, bad_temp, &bad_fsize, &bad_ftime);
  203.                     if (strcmp(xfer_real,bad_real) ||
  204.                         strcmp(xfer_temp,bad_temp) ||
  205.                         xfer_fsize != bad_fsize || xfer_ftime != bad_ftime) {
  206.                        fputs(linebuf,new_fp);
  207.                        left = true;
  208.                     }
  209.               }
  210.               fclose(fp);
  211.               fclose(new_fp);
  212.               unlink(xfer_log);
  213.               if (left) rename(new_log,xfer_log);
  214.               else      unlink(new_log);
  215.            }
  216.            else
  217.               fclose(fp);
  218.         }
  219. }/*xfer_del()*/
  220.  
  221. /* end of fmisc.c */
  222.